//
//  InformationCell.m
//  MedicineBox
//
//  Created by dllo on 15/10/5.
//  Copyright (c) 2015年 蓝鸥科技. All rights reserved.
//

#import "InformationCell.h"
#import "DKNightVersion.h"
#import "UIImageView+WebCache.h"
@interface InformationCell ()

@property (nonatomic, retain) UILabel *titleLabel;
@property (nonatomic, retain) UILabel *timeLabel;
@property (nonatomic, retain) UIImageView *imageV;
@property (nonatomic, retain) UILabel *sourceLabel;
@property (nonatomic, retain) UILabel *contentLabel;

@end

@implementation InformationCell

- (void)dealloc
{
    
    
    [_contentLabel release];
    [_titleLabel release];
    [_timeLabel release];
    [_imageV release];
    [_sourceLabel release];
    [super dealloc];
    
    
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, [[UIScreen mainScreen] bounds].size.width - 20, 30)];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.font = [UIFont fontWithName:@"Baskerville-SemiBold" size:18];
        _timeLabel.nightTextColor = [UIColor whiteColor];
        [self.contentView addSubview:_titleLabel];
        [_titleLabel release];
        
        self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, _titleLabel.frame.origin.y + _titleLabel.frame.size.height, [[UIScreen mainScreen] bounds].size.width / 2, 20)];
        _timeLabel.textAlignment = NSTextAlignmentRight;
        _timeLabel.font = [UIFont systemFontOfSize:10];
        _timeLabel.backgroundColor = [UIColor clearColor];
        _timeLabel.nightTextColor = [UIColor whiteColor];
        [self.contentView addSubview:_timeLabel];
        [_timeLabel release];
        
        self.sourceLabel = [[UILabel alloc] initWithFrame:CGRectMake([[UIScreen mainScreen] bounds].size.width / 2 + 2, _timeLabel.frame.origin.y, [[UIScreen mainScreen] bounds].size.width / 2 - 2, 20)];
        _sourceLabel.textAlignment = NSTextAlignmentLeft;
        _sourceLabel.backgroundColor = [UIColor clearColor];
        _sourceLabel.nightTextColor = [UIColor whiteColor];
        _sourceLabel.font = [UIFont systemFontOfSize:12];
        [self.contentView addSubview:_sourceLabel];
        [_sourceLabel release];
        
        self.imageV = [[UIImageView alloc] initWithFrame:CGRectMake(0, _timeLabel.frame.origin.y + _timeLabel.frame.size.height + 5, [[UIScreen mainScreen] bounds].size.width, 140)];
        _imageV.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:_imageV];
        [_imageV release];
        
        self.contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(5, _imageV.frame.origin.y + _imageV.frame.size.height + 5 ,[[UIScreen mainScreen] bounds].size.width - 10, 200)];
        _contentLabel.textAlignment = NSTextAlignmentCenter;
        _contentLabel.numberOfLines = 0;
        _contentLabel.nightTextColor = [UIColor whiteColor];
        _contentLabel.backgroundColor = [UIColor clearColor];
        [self.contentView addSubview:_contentLabel];
        [_contentLabel release];
        
        
    }
    return self;
}

- (void)getDic:(NSDictionary *)dic
{

    _titleLabel.text = [dic objectForKey:@"CategoryName"];
    _timeLabel.text = [dic objectForKey:@"NewsDate"];
    _sourceLabel.text = [NSString stringWithFormat:@"来源于 %@",[dic objectForKey:@"NewsSource"]];
    [_imageV sd_setImageWithURL:[dic objectForKey:@"NewsPicUrl"] placeholderImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"e4ba16eb86ada6d3944bcd8087b94fbe" ofType:@"jpg"]]];
  
    NSMutableString *str = [NSMutableString stringWithString:[dic objectForKey:@"NewsDetail"]];
    NSRange range = NSMakeRange(0, str.length);
    [str replaceOccurrencesOfString:@"<p>" withString:@"    " options:NSCaseInsensitiveSearch range:range];
     range = NSMakeRange(0, str.length);
    [str replaceOccurrencesOfString:@"</p>" withString:@"\n" options:NSCaseInsensitiveSearch range:range];
    range = NSMakeRange(0, str.length);
    [str replaceOccurrencesOfString:@"&ldquo;" withString:@"" options: NSCaseInsensitiveSearch range:range];
    range = NSMakeRange(0, str.length);
    [str replaceOccurrencesOfString:@"&rdquo;" withString:@"" options: NSCaseInsensitiveSearch range:range];
    
    range = NSMakeRange(0, str.length);
    [str replaceOccurrencesOfString:@"&rsquo" withString:@"" options:NSCaseInsensitiveSearch range:range];
    range = NSMakeRange(0, str.length);
    [str replaceOccurrencesOfString:@"&lsquo" withString:@"" options:NSCaseInsensitiveSearch range:range];
    range = NSMakeRange(0, str.length);
    [str replaceOccurrencesOfString:@"<br />" withString:@"\n" options:NSCaseInsensitiveSearch range:range];
   NSArray *array = [str componentsSeparatedByString:@"&"];
    str = [array firstObject];
    
    CGFloat height = [self getContentHeight:[dic objectForKey:@"NewsDetail"]];
    _contentLabel.frame = CGRectMake(5, _imageV.frame.origin.y + _imageV.frame.size.height + 5 ,[[UIScreen mainScreen] bounds].size.width - 10, height);
    
    NSMutableParagraphStyle *para = [[NSMutableParagraphStyle alloc] init];
    para.alignment = NSTextAlignmentLeft;
    para.maximumLineHeight = 20.f;
    para.minimumLineHeight = 10.f;
    para.firstLineHeadIndent = 20.f;
    NSAttributedString *attStr = [[NSAttributedString alloc] initWithString:str attributes:@{NSParagraphStyleAttributeName:para}];
    _contentLabel.attributedText = attStr;
    

}

- (CGFloat)getContentHeight:(NSString *)text
{

    CGSize size = CGSizeMake([[UIScreen mainScreen] bounds].size.width - 10, 10000);
   CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:17],NSFontAttributeName, nil] context:nil];
    return rect.size.height;

}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
